/* $Id : shopping_flow.js 4865 2007-01-31 14:04:10Z paulgao $ */

var selectedShipping = null;
var selectedPayment  = null;
var selectedPack     = null;
var selectedCard     = null;
var selectedSurplus  = '';
var selectedBonus    = 0;
var selectedIntegral = 0;
var selectedOOS      = null;
var alertedSurplus   = false;


var groupBuyShipping = null;
var groupBuyPayment  = null;


/* *
 * 改变配送方式
 */
function selectShipping(obj)
{
  if (selectedShipping == obj)
  {
    return;
  }
  else
  {
    selectedShipping = obj;
  }

  var supportCod = obj.attributes['supportCod'].value + 0;
  var theForm = obj.form;

  for (i = 0; i < theForm.elements.length; i ++ )
  {
    if (theForm.elements[i].name == 'payment' && theForm.elements[i].attributes['isCod'].value == '1')
    {
      if (supportCod == 0)
      {
        theForm.elements[i].checked = false;
        theForm.elements[i].disabled = true;
      }
      else
      {
        theForm.elements[i].disabled = false;
      }
    }
  }

  if (obj.attributes['insure'].value + 0 == 0)
  {
    document.getElementById('ECS_NEEDINSURE').checked = false;
    document.getElementById('ECS_NEEDINSURE').disabled = true;
  }
  else
  {
    document.getElementById('ECS_NEEDINSURE').checked = false;
    document.getElementById('ECS_NEEDINSURE').disabled = false;
  }

  var now = new Date();
  Ajax.call('flow.php?step=select_shipping', 'shipping=' + obj.value, orderShippingSelectedResponse, 'GET', 'JSON');
}

/**
 *
 */
function orderShippingSelectedResponse(result)
{
  if (result.need_insure)
  {
    try
    {
      document.getElementById('ECS_NEEDINSURE').checked = true;
    }
    catch (ex)
    {
      alert(ex.message);
    }
  }

  try
  {
    if (document.getElementById('ECS_CODFEE') != undefined)
    {
      document.getElementById('ECS_CODFEE').innerHTML = result.cod_fee;
    }
  }
  catch (ex)
  {
    alert(ex.message);
  }

  orderSelectedResponse(result);
}

/* *
 * 改变支付方式
 */
function selectPayment(obj)
{
  if (selectedPayment == obj)
  {
    return;
  }
  else
  {
    selectedPayment = obj;
  }

  Ajax.call('flow.php?step=select_payment', 'payment=' + obj.value, orderSelectedResponse, 'GET', 'JSON');
}
/* *
 * 团购购物流程 --> 改变配送方式
 */
function handleGroupBuyShipping(obj)
{
  if (groupBuyShipping == obj)
  {
    return;
  }
  else
  {
    groupBuyShipping = obj;
  }

  var supportCod = obj.attributes['supportCod'].value + 0;
  var theForm = obj.form;

  for (i = 0; i < theForm.elements.length; i ++ )
  {
    if (theForm.elements[i].name == 'payment' && theForm.elements[i].attributes['isCod'].value == '1')
    {
      if (supportCod == 0)
      {
        theForm.elements[i].checked = false;
        theForm.elements[i].disabled = true;
      }
      else
      {
        theForm.elements[i].disabled = false;
      }
    }
  }

  if (obj.attributes['insure'].value + 0 == 0)
  {
    document.getElementById('ECS_NEEDINSURE').checked = false;
    document.getElementById('ECS_NEEDINSURE').disabled = true;
  }
  else
  {
    document.getElementById('ECS_NEEDINSURE').checked = false;
    document.getElementById('ECS_NEEDINSURE').disabled = false;
  }

  Ajax.call('group_buy.php?act=select_shipping', 'shipping=' + obj.value, orderSelectedResponse, 'GET');
}

/* *
 * 团购购物流程 --> 改变支付方式
 */
function handleGroupBuyPayment(obj)
{
  if (groupBuyPayment == obj)
  {
    return;
  }
  else
  {
    groupBuyPayment = obj;
  }

  Ajax.call('group_buy.php?act=select_payment', 'payment=' + obj.value, orderSelectedResponse, 'GET');
}

/* *
 * 改变商品包装
 */
function selectPack(obj)
{
  if (selectedPack == obj)
  {
    return;
  }
  else
  {
    selectedPack = obj;
  }

  Ajax.call('flow.php?step=select_pack', 'pack=' + obj.value, orderSelectedResponse, 'GET', 'JSON');
}

/* *
 * 改变祝福贺卡
 */
function selectCard(obj)
{
  if (selectedCard == obj)
  {
    return;
  }
  else
  {
    selectedCard = obj;
  }

  Ajax.call('flow.php?step=select_card', 'card=' + obj.value, orderSelectedResponse, 'GET', 'JSON');
}

/* *
 * 选定了配送保价
 */
function selectInsure(needInsure)
{
  needInsure = needInsure ? 1 : 0;

  Ajax.call('flow.php?step=select_insure', 'insure=' + needInsure, orderSelectedResponse, 'GET', 'JSON');
}

/* *
 * 团购购物流程 --> 选定了配送保价
 */
function handleGroupBuyInsure(needInsure)
{
  needInsure = needInsure ? 1 : 0;

  Ajax.call('group_buy.php?act=select_insure', 'insure=' + needInsure, orderSelectedResponse, 'GET', 'JSON');
}

/* *
 * 回调函数
 */
function orderSelectedResponse(result)
{
  if (result.error)
  {
    alert(result.error);
    location.href = './';
  }

  try
  {
    var layer = document.getElementById("ECS_ORDERTOTAL");

    layer.innerHTML = (typeof result == "object") ? result.content : result;

    if (result.payment != undefined)
    {
      var surplusObj = document.forms['theForm'].elements['surplus'];
      if (surplusObj != undefined)
      {
        surplusObj.disabled = result.pay_code == 'balance';
      }
    }
  }
  catch (ex) { }
}

/* *
 * 改变余额
 */
function changeSurplus(val)
{
  if (selectedSurplus == val)
  {
    return;
  }
  else
  {
    selectedSurplus = val;
  }

  Ajax.call('flow.php?step=change_surplus', 'surplus=' + val, changeSurplusResponse, 'GET', 'JSON');
}

/* *
 * 改变余额回调函数
 */
function changeSurplusResponse(obj)
{
  if (obj.error)
  {
    try
    {
      document.getElementById("ECS_SURPLUS_NOTICE").innerHTML = obj.error;
      document.getElementById('ECS_SURPLUS').value = '0';
      document.getElementById('ECS_SURPLUS').focus();
    }
    catch (ex) { }
  }
  else
  {
    try
    {
      document.getElementById("ECS_SURPLUS_NOTICE").innerHTML = '';
    }
    catch (ex) { }
    orderSelectedResponse(obj.content);
  }
}

/* *
 * 改变积分
 */
function changeIntegral(val)
{
  if (selectedIntegral == val)
  {
    return;
  }
  else
  {
    selectedIntegral = val;
  }

  Ajax.call('flow.php?step=change_integral', 'points=' + val, changeIntegralResponse, 'GET', 'JSON');
}

/* *
 * 改变积分回调函数
 */
function changeIntegralResponse(obj)
{
  if (obj.error)
  {
    try
    {
      document.getElementById('ECS_INTEGRAL_NOTICE').innerHTML = obj.error;
      document.getElementById('ECS_INTEGRAL').value = '0';
      document.getElementById('ECS_INTEGRAL').focus();
    }
    catch (ex) { }
  }
  else
  {
    try
    {
      document.getElementById('ECS_INTEGRAL_NOTICE').innerHTML = '';
    }
    catch (ex) { }
    orderSelectedResponse(obj.content);
  }
}

/* *
 * 改变红包
 */
function changeBonus(val)
{
  if (selectedBonus == val)
  {
    return;
  }
  else
  {
    selectedBonus = val;
  }
  shipping_method = get_shipping_method();
  var is_surplus = 0;
  if(document.getElementById('is_surplus').checked==true)
  {
	  is_surplus = 1;
  } 
  Ajax.call('flow.php?step=change_bonus', 'bonus=' + val+"&shipping_method="+shipping_method+"&is_surplus="+is_surplus, changeBonusResponse, 'GET', 'JSON');
}

/* *
 * 改变红包的回调函数
 */
function changeBonusResponse(obj)
{
  if (obj.error)
  {
    alert(obj.error);
	
	document.getElementById('is_usebonux').checked=false;
	var   a   =   document.getElementsByName("bonus");
    for   (var   i=0;   i<a.length;   i++)   
    {   
       a[i].checked=false;   
    }
	
    try
    {
      document.getElementById('ECS_BONUS').value = '0';
    }
    catch (ex) { }
  }
  else
  {
    orderSelectedResponse(obj.content);
  }
}

/**
 * 验证红包序列号
 * @param string bonusSn 红包序列号
 */
function validateBonus(bonusSn)
{
  Ajax.call('flow.php?step=validate_bonus', 'bonus_sn=' + bonusSn, validateBonusResponse, 'GET', 'JSON');
}

function validateBonusResponse(obj)
{
  if (obj.error)
  {
    alert(obj.error);
    //orderSelectedResponse(obj.content);
    //try
    //{
      //document.getElementById('ECS_BONUSN').value = '0';
    //}
    //catch (ex) { }
  }
  else
  {
	  document.getElementById('bonus_list_info').innerHTML = obj.bonus_list_info;
/*	  
	 //document.getElementById('ECS_BONUS').innerHTML = obj.bonus_list;   
    //orderSelectedResponse(obj.content);
	var sel = document.getElementById('ECS_BONUS');
	
	//clear list items
	while (sel.options.length>0)
	{
		sel.options[0] = null;
	}
	if (obj.bonus_list)
	{
	  //add items to list
	  for (i = 0; i < obj.bonus_list.length; i ++ )
	  {
		  var opt = document.createElement("OPTION");
		  opt.value = obj.bonus_list[i].value;
		  opt.text  = obj.bonus_list[i].text;
		  sel.options.add(opt);
	  }
	}
*/	
  }
  document.getElementById('ECS_BONUSN').value = '';
}

/* *
 * 改变发票的方式
 */
function changeNeedInv()
{
  var obj        = document.getElementById('ECS_NEEDINV');
  var objType    = document.getElementById('ECS_INVTYPE');
  var objPayee   = document.getElementById('ECS_INVPAYEE');
  var objContent = document.getElementById('ECS_INVCONTENT');
  var needInv    = obj.checked ? 1 : 0;
  var invType    = obj.checked ? (objType != undefined ? objType.value : '') : '';
  var invPayee   = obj.checked ? objPayee.value : '';
  var invContent = obj.checked ? objContent.value : '';
  objType.disabled = objPayee.disabled = objContent.disabled = ! obj.checked;
  if(objType != null)
  {
    objType.disabled = ! obj.checked;
  }

  Ajax.call('flow.php?step=change_needinv', 'need_inv=' + needInv + '&inv_type=' + encodeURIComponent(invType) + '&inv_payee=' + encodeURIComponent(invPayee) + '&inv_content=' + encodeURIComponent(invContent), orderSelectedResponse, 'GET');
}

/* *
 * 改变发票的方式
 */
function groupBuyChangeNeedInv()
{
  var obj        = document.getElementById('ECS_NEEDINV');
  var objPayee   = document.getElementById('ECS_INVPAYEE');
  var objContent = document.getElementById('ECS_INVCONTENT');
  var needInv    = obj.checked ? 1 : 0;
  var invPayee   = obj.checked ? objPayee.value : '';
  var invContent = obj.checked ? objContent.value : '';
  objPayee.disabled = objContent.disabled = ! obj.checked;

  Ajax.call('group_buy.php?act=change_needinv', 'need_idv=' + needInv + '&amp;payee=' + invPayee + '&amp;content=' + invContent, null, 'GET');
}

/* *
 * 改变缺货处理时的处理方式
 */
function changeOOS(obj)
{
  if (selectedOOS == obj)
  {
    return;
  }
  else
  {
    selectedOOS = obj;
  }

  Ajax.call('flow.php?step=change_oos', 'oos=' + obj.value, null, 'GET');
}

/* *
 * 检查提交的订单表单
 */
function checkOrderForm(frm)
{
  var shipping_method;
  var temp;
  var i;
  var seletedValue;

  temp=document.getElementsByName("shipping_method");

  for (i=0;i<temp.length;i++)
  {
    if(temp[i].checked) shipping_method=temp[i].value;
  }
  if(shipping_method == 'self_pickup' && !document.theForm.selAddress)
  {
        alert('请选择有效的自提地址！');
    return false;
  }

  if(shipping_method == 'express' && !document.theForm.selAddress2)
  {
        alert('请填写有效的送货地址！');
    return false;
  }
   for(i=0;i<document.theForm.shipping_date_style.length;i++)
   {
      if(document.theForm.shipping_date_style[i].checked)
      {
       seletedValue = document.theForm.shipping_date_style[i].value;
      }
   }


  if (seletedValue=="customized" && document.theForm.unit_ok.value == "0")
  {
        alert('自选的配送单位有误，请检查');
    return false;
  }

  document.theForm.confirm_submit.disabled = 'disabled';
  frm.action = frm.action + '?step=done';
  return true;
}


/* *
 * 检查收货地址信息表单中填写的内容
 */
function checkConsignee(frm)
{
  var msg = new Array();
  var err = false;

  if (frm.elements['country'] && frm.elements['country'].value == 0)
  {
    msg.push(country_not_null);
    err = true;
  }

  if (frm.elements['province'] && frm.elements['province'].value == 0 && frm.elements['province'].length > 1)
  {
    err = true;
    msg.push(province_not_null);
  }

  if (frm.elements['city'] && frm.elements['city'].value == 0 && frm.elements['city'].length > 1)
  {
    err = true;
    msg.push(city_not_null);
  }

  if (frm.elements['district'] && frm.elements['district'].length > 1)
  {
    if (frm.elements['district'].value == 0)
    {
      err = true;
      msg.push(district_not_null);
    }
  }

  if (Utils.isEmpty(frm.elements['consignee'].value))
  {
    err = true;
    msg.push(consignee_not_null);
  }

  if ( ! Utils.isEmail(frm.elements['email'].value))
  {
    err = true;
    msg.push(invalid_email);
  }

  if (frm.elements['address'] && Utils.isEmpty(frm.elements['address'].value))
  {
    err = true;
    msg.push(address_not_null);
  }

  if (frm.elements['zipcode'] && frm.elements['zipcode'].value.length > 0 && (!Utils.isNumber(frm.elements['zipcode'].value)))
  {
    err = true;
    msg.push(zip_not_num);
  }

  if (Utils.isEmpty(frm.elements['tel'].value))
  {
    err = true;
    msg.push(tele_not_null);
  }
  else
  {
    if (!Utils.isTel(frm.elements['tel'].value))
    {
      err = true;
      msg.push(tele_invaild);
    }
  }

  if (frm.elements['mobile'] && frm.elements['mobile'].value.length > 0 && (!Utils.isTel(frm.elements['mobile'].value)))
  {
    err = true;
    msg.push(mobile_invaild);
  }

  if (err)
  {
    message = msg.join("\n");
    alert(message);
  }
  return ! err;
}


/* added by mengyin at 2011-07-12 */

/* *
 * 根据指定门店详细信息
 */

function showStoreInfo(obj,target)
{
		var store_id = obj.options[obj.selectedIndex].value;
		
	  var sel = document.getElementById(target);

		if (store_id.length>2&&store_id>0)
		{
			sel.style.display='';
		
			try
			{
				Ajax.call('flow.php?step=showStoreInfo', 'store_id=' + store_id+'&target='+target, showStoreInfoResponse, 'GET','JSON');
			}
			catch (ex)
			{
					alert(ex.message);
			}
		}
		else 
		{
			sel.style.display='none';
		}
		return false;

}
function showStoreInfoResponse(result)
{
  //var sel = document.getElementById(result.target);
  //sel.innerHTML = result.storeinfo;
  document.getElementById("mapinfo").innerHTML = result.mapinfo;
  document.getElementById("StoreName").innerHTML = result.StoreName;
  document.getElementById("Address").innerHTML = result.Address;
  document.getElementById("Tel").innerHTML = result.Tel;
}

/* *
 * 根据区域标识显示门店列表
 */

function getStoreList(obj, type, selName)
{

  var region_id = obj.options[obj.selectedIndex].value;
  if(region_id>0)
 {
	  try
	  {
		  Ajax.call('flow.php?step=getStoreList', 'region_id=' + region_id+'&type='+type+'&target='+selName, getStoreListResponse, 'GET','JSON');
	  }
	  catch (ex)
	  {
			  alert(ex.message);
	  }
  }
  return false;


}

function getStoreListResponse(result)
{
  var sel = document.getElementById(result.target);

	//clear list items
	while (sel.options.length>0)
	{
		sel.options[0] = null;
	}
  if (result.store_list)
  {
	  //add items to list
    for (i = 0; i < result.store_list.length; i ++ )
    {
      var opt = document.createElement("OPTION");
      opt.value = result.store_list[i].StoreID;
      opt.text  = result.store_list[i].StoreName;

      sel.options.add(opt);
    }
  }
  if (result.store_list.length == 0)
  {
      var opt = document.createElement("OPTION");
      opt.value = 0;
      opt.text  = "本地区暂无门店";
      sel.options.add(opt);
  }
  
  //document.getElementById("store_info").innerHTML = result.storeinfo;
  document.getElementById("mapinfo").innerHTML = result.mapinfo;
  document.getElementById("StoreName").innerHTML = result.StoreName;
  document.getElementById("Address").innerHTML = result.Address;
  document.getElementById("Tel").innerHTML = result.Tel;

}

function get_map_store(lat, lng)
{

	try
	{
		Ajax.call('flow.php?step=get_map_store&lat='+lat+'&lng='+lng, '', get_map_storeResponse, 'GET','JSON');
	}
	catch (ex)
	{
			alert(ex.message);
	}
	return false;
}
function get_map_storeResponse(result)
{
	if(result.error=="1")
	{
		alert(result.message);
		return;
	}

	parent.document.getElementById("StoreID").value = result.StoreID;
	parent.document.getElementById("StoreName").innerHTML = result.StoreName;
	parent.document.getElementById("Address").innerHTML = result.Address;
	parent.document.getElementById("Tel").innerHTML = result.Tel;
	
	
	parent.document.all.district.value=result.DistrictID;

  var selStores = parent.document.getElementById('StoreID');
 
  selStores.options.length = 1;
  
  for(var i=0; i<result.store_list.length; i++) {
	 var opt = parent.document.createElement("OPTION");
	 opt.value = result.store_list[i].StoreID; 
	 opt.text = result.store_list[i].StoreName;
	 selStores.options.add(opt);
	 if(result.StoreID == opt.value) {
		 opt.selected = 'selected';
	 }
  }
}

function dropselfpickupstore(rec_id)
{
	if(confirm("真的要删除吗？")==false)
		return;
	var target="div_selfpickup";
	document.getElementById(target).style.display='';
	var request_string='action=dropselfpickupstore'+'&rec_id='+rec_id+'&target='+target;
	processConsignee(request_string);	
}
function other_address(display)
{
  //显示其它地址	
  if(document.getElementById('address_list_others'))
  {
	  document.getElementById('address_list_others').style.display='none';
  }
  document.getElementById('address_title').innerHTML = '编辑提货地址';
  document.getElementById('address_id').value='0';
  //显示添加新地址界面，是否显示取决于当前地址是否已经有5个
  document.getElementById('map_info').style.display=display;
}

function other_address2(display)
{
  //显示其它地址	
  if(document.getElementById('address_list_others2'))
  {
	  document.getElementById('address_list_others2').style.display='block';
  }
  document.getElementById('address_id2').value='0';
  //显示添加新地址界面，是否显示取决于当前地址是否已经有5个
  document.getElementById('express_info').style.display=display;
}
function addstore(fromwhere, flag)
{
	var district = document.getElementById("district").value;
	var store_id = document.getElementById("StoreID").value;
	var consignee = document.getElementById("consignee").value;
	var mobile = document.getElementById("mobile").value;
	var address_id = document.getElementById("address_id").value;
	if(consignee == '')
	{
		alert("您还没有填写提货人！");
		document.getElementById("consignee").focus();
		return;
	}
	if(mobile == '')
	{
		alert("您还没有填写提货人手机号！");
		document.getElementById("mobile").focus();
		return;
	}
	
	if(!(/^1[358]\d{9}$/.test(mobile)))
    {
		alert("手机号码错误！为了能收到提货码，请填写正确手机号！！");
		document.getElementById("mobile").focus();
		return;		
    }
	var request_string='action=addstore'+'&district='+district+'&store_id='+store_id+'&consignee='+consignee+'&mobile='+mobile+'&address_id='+address_id+'&fromwhere='+fromwhere;
    if(!flag) {
        document.getElementById('address_title').innerHTML = '自提门店';
    }
	processConsignee(request_string);	
}

function addaddress(fromwhere, flag)
{
	var district2 = document.getElementById("district2").value;
	var address2 = document.getElementById("address2").value;
	var mobile2 = document.getElementById("mobile2").value;
	var consignee2 = document.getElementById("consignee2").value;
	var address_id2 = document.getElementById("address_id2").value;
	if(consignee2 == '' || consignee2 == '请准确填写真实姓名')
	{
		alert("您还没有填写收货人！");
		document.getElementById("consignee2").focus();
		return;
	}
	if(address2 == '' || address2 == '请填写详细路名或门牌号码')
	{
		alert("您还没有填写提货地址！");
		document.getElementById("address2").focus();
		return;
	}
	if(mobile2 == '' || mobile2 == '请准确填写手机号')
	{
		alert("您还没有填写收货人手机号！");
		document.getElementById("mobile2").focus();
		return;
	}
	if ( isNaN(mobile2) )
    {
		alert("手机号码错误！");
		document.getElementById("mobile2").focus();
		return;		
    }
	var request_string='action=addaddress'+'&district='+district2+'&consignee='+consignee2+'&mobile='+mobile2+'&address='+address2+'&address_id='+address_id2+'&fromwhere='+fromwhere;
	processConsignee2(request_string);
    if(!flag){

    }else{
        getEle('.cartBox')[0].style.display = 'block';
        getEle('.edit_address')[0].style.display = 'none';
        document.getElementById('address_title').innerHTML = '常用收货地址';
    }

}

function dropstore(address_id,fromwhere)
{
	if(confirm("真的要删除吗？")==false)
		return;	
	var request_string='action=dropstore'+'&address_id='+address_id+'&fromwhere='+fromwhere;
	processConsignee(request_string);	
}

function modifystore(address_id,fromwhere)
{
	var request_string='action=modifystore'+'&address_id='+address_id+'&fromwhere='+fromwhere;
	processConsignee(request_string);	
}

function modifyaddress(address_id,fromwhere)
{
	var request_string='action=modifyaddress'+'&address_id='+address_id+'&fromwhere='+fromwhere;
	processConsignee2(request_string);	
}

function dropaddress(address_id,fromwhere)
{
	if(confirm("真的要删除吗？")==false)
		return;	
	var request_string='action=dropaddress'+'&address_id='+address_id+'&fromwhere='+fromwhere;
	processConsignee2(request_string);	
}
//点击默认地址时，跳转
function set_default_old(){
    // alert("设置成功！");
    var u = document.referrer;
    if(u == 'http://m.e24c.com/flow.php?step=checkout'){
      window.location.href='http://m.e24c.com/flow.php?step=checkout';
    }

    if(u == 'http://m.e24c.com/flow_new.php?step=checkout'){
      window.location.href='http://m.e24c.com/flow_new.php?step=checkout';
    }
}
function set_default_address(style,address_id)
{
	try
	{
		Ajax.call('flow.php?step=set_default_address', "&style="+style+"&address_id="+address_id, set_default_addressResponse, 'GET','JSON');
	}
	catch (ex)
	{
			alert(ex.message);
	}
	return false;
}
function set_default_addressResponse(result)
{

	if(result.error=="0")
	{
		// alert("设置成功！");
    var u = document.referrer;
    if(u == 'http://m.e24c.com/flow.php?step=checkout'){
      window.location.href='http://m.e24c.com/flow.php?step=checkout';
    }
    if(u == 'http://m.e24c.com/flow_new.php?step=checkout'){
      window.location.href='http://m.e24c.com/flow_new.php?step=checkout';
    }
		return;
	}	

}



function processConsignee(request_string)
{
	try
	{
		Ajax.call('flow.php?step=aconsignee', request_string, processConsigneeResponse, 'GET','JSON');
	}
	catch (ex)
	{
			alert(ex.message);
	}
	return false;
}
function processConsigneeResponse(result)
{
	if(result.error=="storealreadyin")
	{
		alert("该门店已经在您的提货信息列表中");
		return;
	}
	if(result.error=="wrongnamemobile")
	{
		alert("提货人信息无效");
		return;
	}
	if(result.error=="toomanystores")
	{
		alert("最多只能添加500条提货信息。");
		return;
	}	
    // var u = document.referrer;
    // if(u == 'http://m.e24c.com/flow.php?step=checkout'){
    //   window.location.href='http://m.e24c.com/flow.php?step=checkout';
    // }

    // $.post('flow.php?step=check_order_num', {v:1}, function(data) {
    //   var u = document.referrer;
    //   if(data == 'ok' && u == 'http://m.e24c.com/flow.php?step=checkout'){
    //     window.location.href='http://m.e24c.com/flow.php?step=checkout';
    //   }
    // });
    document.getElementById("consigneeinfo").innerHTML = result.consigneeinfo;

}

function processConsignee2(request_string)
{
	try
	{
		Ajax.call('flow.php?step=aconsignee2', request_string, processConsignee2Response, 'GET','JSON');
	}
	catch (ex)
	{
			alert(ex.message);
	}
	return false;
}
function processConsignee2Response(result)
{
	if(result.error=="wrongnamemobile")
	{
		alert("收货人信息无效");
		return;
	}
	if(result.error=="toomanystores")
	{
		alert("最多只能添加5条收货信息。");
		return;
	}
    document.getElementById("consigneeinfo2").innerHTML = result.consigneeinfo;
}

function get_shipping_method()
{
    var i;
    var temp;
	var shipping_method;
    temp=document.getElementsByName("shipping_method");
    for (i=0;i<temp.length;i++)
    {
      if(temp[i].checked) shipping_method=temp[i].value;
    }
	return shipping_method;
}

function get_shipping_fee(shipping_times)
{
	var shippingfee;
	if(shipping_times==1) shippingfee= 10;
	else shippingfee=shipping_times * 5;
	
	return shippingfee;
}

//什么情况下调用processFee
//1.改变配送方式时show_shipping_method
//2.选择配送日期时show_shipping_time--去掉
//3.选择使用账户余额时
function processFee()
{

  var shipping_method;
  var shipping_date_style;
  var shipping_times;
  var shippingfee;
  var i;
  var temp;
  
  var isuseSurplus='no';
  obj=document.getElementById('is_surplus');
  if(obj && obj.checked==true) //预约兑换的流程，没有is_surplus
  {
    isuseSurplus='yes';
  }
  
  shipping_method = get_shipping_method();
  temp=document.getElementsByName("shipping_date_style");
  for (i=0;i<temp.length;i++)
  {
    if(temp[i].checked) shipping_date_style=temp[i].value;
  }

  if(shipping_date_style=='system_default')
  {
	 shipping_times =  document.getElementById("system_default_shipping_times").innerHTML;
  }
  else
  {
	 shipping_times =  document.getElementById("customized_shipping_times").innerHTML;
  }
  
  try
  {
	  Ajax.call('flow.php?step=processFee', "&isuseSurplus="+isuseSurplus+"&shipping_method="+shipping_method, processFeeResponse, 'GET','JSON');
  }
  catch (ex)
  {
	  alert(ex.message);
  }
	return false;
}
function processFeeResponse(obj)
{
  if (obj.error)
  {
    alert(obj.error);
  }
  else
  {	
	 document.getElementById("shipping_fee_show").innerHTML =  obj.shipping_fee + " 元";
		if(obj.first_shipping_fee > 0){
			document.getElementById("shipping_fee_show").innerHTML =  obj.shipping_fee + " 元，减免 " + obj.first_shipping_fee_formated + " 元";
		}
    orderSelectedResponse(obj.content);
    //document.getElementById("system_default_shipping_fee").innerHTML = obj.shippingfee;
	//document.getElementById("customized_shipping_fee").innerHTML = obj.shippingfee;
	document.getElementById('customized_shipping_fee').innerHTML = obj.shipping_fee;
	document.getElementById('surplus').value = obj.surplus;
	
  }
}

function update_goods_number(rec_id)
{

    var value = parseInt(document.getElementById('goods_number_'+rec_id).value);
	//alert('flow.php?step=update_goods_number&rec_id='+rec_id+'&value='+value);
	try
	{
		Ajax.call('flow.php?step=update_goods_number&rec_id='+rec_id+'&value='+value, '', update_goods_numberResponse, 'GET','JSON');
	}
	catch (ex)
	{
			alert(ex.message);
	}
	return false;
}
function update_goods_numberResponse(result)
{
//define('ERR_NOT_EXISTS',            1); // 商品不存在
//define('ERR_OUT_OF_STOCK',          2); // 商品缺货
//define('ERR_NOT_ON_SALE',           3); // 商品已下架
//define('ERR_CANNT_ALONE_SALE',      4); // 商品不能单独销售
//define('ERR_NO_BASIC_GOODS',        5); // 没有基本件
//define('ERR_NEED_SELECT_ATTR',      6); // 需要用户选择属性

//	alert('aa'+result.error+'bb');
	if(result.error=="1" || result.error=="2" || result.error=="3" || result.error=="4" || result.error=="5" || result.error=="7")
{
    if(result.error=="1")
	{
		alert("商品不存在，更新失败！");
	}
    if(result.error=="2")
	{
		alert("库存不足，更新失败！");
	}
    if(result.error=="3")
	{
		alert("商品已下架，更新失败！");
	}
    if(result.error=="4")
	{
		alert("商品不能单独销售，更新失败！");
	}
    if(result.error=="5")
	{
		alert("没有基本件，更新失败！");
	}
    if(result.error=="7")
	{
		alert("超出购买限制，更新失败！");
	}
	document.getElementById('goods_number_'+result.rec_id).value = result.goods_number;  //更新失败的时候恢复为更改前的商品数量
	return;
}

    document.getElementById('goods_subtotal_'+result.rec_id).innerHTML = result.goods_subtotal;
    document.getElementById('shopping_number').innerHTML = result.shopping_number;
    document.getElementById('shopping_money').innerHTML = result.shopping_money;

    var cartInfo = document.getElementById('ECS_CARTINFO');

    if (cartInfo)
    {
      cartInfo.innerHTML = result.content;
    }

}


function show_shipping_time(style)
{
  if(style==1)
  {
    document.getElementById('customized_shipping_list').style.display='none';
    document.getElementById('select_customized_shipping_list').style.display='none';
    document.getElementById('customized_summary').style.display='none';
  }
  else
  {
    document.getElementById('system_default_shipping_list').style.display='none';
    document.getElementById('select_customized_shipping_list').style.display='block';
    document.getElementById('customized_summary').style.display='block';
  }
  
  var shipping_method;
  var i;
  var temp;
  
  temp=document.getElementsByName("shipping_method");
  for (i=0;i<temp.length;i++)
  {
    if(temp[i].checked) shipping_method=temp[i].value;
  }
  
  //if(shipping_method=="self_pickup") return;  
  
  //processFee();
  
}
function show_system_default_shipping_list(style)
{
  if(style==1)
  {
    document.getElementById('system_default_shipping_list').style.display='block';
    document.getElementById('control1').innerHTML='<a href="javascript:show_system_default_shipping_list(0);" class="f6">点击收回>></a>';	
  }
  else
  {
    document.getElementById('system_default_shipping_list').style.display='none';
    document.getElementById('control1').innerHTML='<a href="javascript:show_system_default_shipping_list(1);" class="f6">点击查看>></a>';	
  }
}

function show_customized_shipping_list(style)
{
  if(style==1)
  {
    document.getElementById('customized_shipping_list').style.display='block';
    document.getElementById('control2').innerHTML='<a href="javascript:show_customized_shipping_list(0);" class="f6">点击收回</a>';	
  }
  else
  {
    document.getElementById('customized_shipping_list').style.display='none';
    document.getElementById('control2').innerHTML='<a href="javascript:show_customized_shipping_list(1);" class="f6">点击查看>></a>';	
  }
}


function show_goods_customized_shipping_list(goods_id,shipping_times,style)
{
  if(style==1)
  {
    if(shipping_times!=1)
	{
	  document.getElementById('shipping_date_text_'+goods_id).innerHTML='配送日期：';
	}
    document.getElementById('goods_customized_shipping_date_'+goods_id).style.display='none';
    document.getElementById('goods_customized_shipping_list_'+goods_id).style.display='block';
    document.getElementById('control_'+goods_id).innerHTML='<a href="javascript:show_goods_customized_shipping_list('+goods_id+','+shipping_times+',0);" class="f6">点击收回>></a>';	
  }
  else
  {
    if(shipping_times!=1)
	{
	  document.getElementById('shipping_date_text_'+goods_id).innerHTML='首次配送：';
	}
    document.getElementById('goods_customized_shipping_date_'+goods_id).style.display='block';
    document.getElementById('goods_customized_shipping_list_'+goods_id).style.display='none';
    document.getElementById('control_'+goods_id).innerHTML='<a href="javascript:show_goods_customized_shipping_list('+goods_id+','+shipping_times+',1);" class="f6">点击查看>></a>';	
  }
}

function change_unit(goods_id,weekly_num,from,order_id)
{
    var unit_2 = parseInt(document.getElementById('unit_2_'+goods_id).value);
    var unit_4 = parseInt(document.getElementById('unit_4_'+goods_id).value);
    var unit_6 = parseInt(document.getElementById('unit_6_'+goods_id).value);
	
	if((unit_2+unit_4+unit_6) != weekly_num)
	{
  	  document.getElementById('unit_ok').value="0";
	  if(from=='BUSINESS')
	  {	
	  	document.getElementById('unit_info_'+goods_id).src="../themes/default/images/unit_error.jpg";
	  }
	  else
	  {
	  	document.getElementById('unit_info_'+goods_id).src="images/unit_error.jpg";
	  }		
	  document.getElementById('unit_info_alert_'+goods_id).innerHTML="总份数必须为"+weekly_num;
	  return;
	}

  	document.getElementById('unit_ok').value="1";
	if(from=='BUSINESS')
	{	
	    document.getElementById('unit_info_'+goods_id).src="../themes/default/images/unit_ok.jpg";
	}
	else
	{		
    	document.getElementById('unit_info_'+goods_id).src="images/unit_ok.jpg";
	}

	document.getElementById('unit_info_alert_'+goods_id).innerHTML="";

    var shipping_method = get_shipping_method();	
  
	if(from=='BUSINESS')
	{		
    Ajax.call('../flow.php?step=customize_shipping', 'goods_id=' + goods_id + '&unit_2=' + unit_2 + '&unit_4=' + unit_4 + '&unit_6=' + unit_6+ '&from=' + from + '&order_id=' + order_id + '&shipping_method=' + shipping_method, customize_shippingResponse, 'GET', 'JSON');
	}
	else
	{		
    Ajax.call('flow.php?step=customize_shipping', 'goods_id=' + goods_id + '&unit_2=' + unit_2 + '&unit_4=' + unit_4 + '&unit_6=' + unit_6+ '&from=' + from + '&order_id=' + order_id + '&shipping_method=' + shipping_method, customize_shippingResponse, 'GET', 'JSON');
	}
}

function customize_date(goods_id,the_time,from,order_id)
{
    var shipping_method = get_shipping_method();
	if(from=='BUSINESS')
	{		
    Ajax.call('../flow.php?step=customize_shipping', 'goods_id=' + goods_id + '&the_time=' + the_time + '&from=' + from + '&order_id=' + order_id + '&shipping_method=' + shipping_method, customize_shippingResponse, 'GET', 'JSON');
	}
	else
	{		
    Ajax.call('flow.php?step=customize_shipping', 'goods_id=' + goods_id + '&the_time=' + the_time + '&from=' + from + '&order_id=' + order_id + '&shipping_method=' + shipping_method, customize_shippingResponse, 'GET', 'JSON');
	}	
}
/**
 *
 */
function customize_shippingResponse(result)
{
  if (result.error==0)
  {
    document.getElementById('customized_shipping_times').innerHTML=result.customized_shipping_times;
    document.getElementById('customized_shipping_fee').innerHTML=result.customized_shipping_fee;
	orderSelectedResponse(result.content);
    document.getElementById('customized_shipping_list').innerHTML=result.customized_shipping_dates_list;
	if(result.customized_shipping_times==1)
	{
      document.getElementById('customized_first_shipping_date').innerHTML='配送日期： '+'<font color="#1290be" style=" font-weight:bold;">'+result.customized_first_shipping_date_f+'</font>';	
	}
	else
	{
      document.getElementById('customized_first_shipping_date').innerHTML='首次配送： '+'<font color="#1290be" style=" font-weight:bold;">'+result.customized_first_shipping_date_f+'</font>';	
	}
	  
	document.getElementById('goods_customized_shipping_times_'+result.goods_id).innerHTML=result.goods_customized_shipping_times;
	document.getElementById('goods_customized_shipping_date_'+result.goods_id).innerHTML=result.goods_customized_shipping_date;
	document.getElementById('goods_customized_shipping_list_'+result.goods_id).innerHTML=result.goods_customized_shipping_list;
	
	if(result.goods_calendar!='')
	{
		document.getElementById('goods_calendar_'+result.goods_id).innerHTML=result.goods_calendar;	
	}
  }
}

/* *
 * 根据区域标识显示门店列表
 */

function update_cart_goods(obj, rec_id,type)
{
   if(type == "shipping_date"){
   	  var value = obj.options[obj.selectedIndex].value;
   }else{
	  var value = obj.value;
   }
   try
   {
	  Ajax.call('flow.php?step=update_cart_goods', 'rec_id=' + rec_id + '&value=' + value +'&field='+ type, update_cart_goods_Response, 'GET','JSON');
   }
   catch (ex)
   {
	  alert(ex.message);
   }
   return false;
}

function update_cart_goods_Response(result)
{
	document.getElementById('shipping_num_'+result.rec_id).innerHTML=result.shipping_times;
	if(result.shipping_times_total==1)
	{
      document.getElementById('customized_first_shipping_date').innerHTML='配送日期： '+'<font color="#1290be" style=" font-weight:bold;">'+result.first_shipping_date+'</font>';	
	}
	else
	{
      document.getElementById('customized_first_shipping_date').innerHTML='首次配送： '+'<font color="#1290be" style=" font-weight:bold;">'+result.first_shipping_date+'</font>';	
	}
	document.getElementById('customized_shipping_times').innerHTML=result.shipping_times_total+"次";
	//document.getElementById('customized_shipping_fee').innerHTML=result.shipping_fee_total;
	processFee();
}